<?php


namespace app\miniProgram\controller;


use app\home\severs\InReceiptSever;
use think\Exception;
use think\facade\Db;

class InReceipt extends Auth
{
    protected $table='in_receipt';
    public  function onSure()
    {
        $validate=new \app\miniProgram\validate\Action();
        if (!$validate->scene('sureInReceipt')->check($this->param)){
            return $this->jump(0,$validate->getError());
        }
        $printCompany=config('config.companyName');
        Db::connect('company')->StartTrans();
        try {
            $wCode=Db::connect('company')->name('warehouse')->where('id',$this->param['wId'])->value('wCode');
            $ordernum=$this->getDJBHBuySql($this->table,'orderNum',$wCode);
            $inData=[
                'type' => $this->param['type'],
                'incomTime' => $this->param['incomTime']??date('Y-m-d'),
                'status' => $this->param['status']??1,
                'printCompany' => $this->param['printCompany']??$printCompany[0],
                'punter_id' => $this->param['punter_id']??'',
                'orderNum'=>$ordernum,
                'create_time'=>time(),
                'create_user'=>$this->userInfo['id'],
                'update_time'=>time(),
            ];
            $id= Db::connect('company')->name($this->table)->strict(false)->field(true)->insertGetId($inData);

            $mxData = [
                'in_receipt_id' => $id,
                'material_dossier_id' => $this->param['mdId'],
                'num' => $this->param['num'],
                'locator' => 1,
                'warehouse_id' => $this->param['wId'],
                'i_remark' => $this->param['i_remark']??'',
                'create_time' => time(),
                'update_time' => time()
            ];
            Db::connect('company')->name('in_receipt_detail')->save($mxData);

            $sures=InReceiptSever::doSure(['id'=>$id],$this->userInfo['id']);
            if ($sures['code']==1){
                throw new Exception($sures['msg']);
            }
            Db::connect('company')->commit();
        }catch (\Exception $e){
            Db::connect('company')->rollback();
            return $this->jump(0,$e->getMessage());
        }

        return $this->jump(1,'操作成功');

}
}